<?php
if (isset($_REQUEST["loginsubmit"]) && isset($_REQUEST["username"]) && isset($_REQUEST["password"]))
{
	$conn = new dbConnection("user");
	$userData = $conn->dbExecuteQuery("SELECT * FROM ta_user WHERE username = '" . $_REQUEST["username"] . "'");
	$loginSuccess = false;
	if ((count($userData) == 1) && ($_REQUEST["password"] == $userData[0]["password"]))
	{
		$loginSuccess = true;
		$_SESSION["username"] = $userData[0]["username"];
		$_SESSION["userid"] = $userData[0]["id"];
		$_SESSION["userType"] = 'user';
		$_SESSION["lastActivityTime"] = time();
		$familyMember = $conn->dbExecuteQuery("SELECT COUNT(*) AS c FROM tx_user_family WHERE userid = " .$_SESSION["userid"]);
		if ($familyMember[0]["c"] > 0)
		{
			$familyData = $conn->dbExecuteQuery("SELECT f.name FROM tx_user_family as uf, ta_family as f WHERE uf.userid = " .$_SESSION["userid"] . " AND uf.familyid = f.id");
			$_SESSION["familyMember"] = $familyData[0]["name"];
		}
		else
		{
			unset($_SESSION["familyMember"]);
		}
	}
}

?>

	<div class="blockheader">
		login
	</div>
	<div class="blockcontent">
<?php
if (isset($_SESSION["userType"]) && ($_SESSION["userType"] != "anonym"))
{
?>
		<form name="logoutForm" method="post" action="<?php print $rootDir;?>/index.php">
			<p>bejelentkezett felhaszáló: <b><?php print $_SESSION["username"];?></b><?php print (isset($_SESSION["familyMember"])?"<br/><i>" . $_SESSION["username"] . " a " . $_SESSION["familyMember"] . " család tagja</i>":"");?></p>
			<input type="submit" name="logout" value="kijelentkezés" class="button"/>
		</form>
<?php
}
else
{
?>
		<form name="loginForm" method="post" action="<?php print $rootDir;?>/index.php">
		<?php print (isset($loginSuccess) && !$loginSuccess)?"<h3 class=\"errortitle\">sikertelen bejelentkezs</h3>":"";?>
			<span class="fieldname">feljasznlónév</span><br />
			<input type="text" name="username" />
			<span class="fieldname">jelszó</span><br />
			<input type="password" name="password" />
			<input type="submit" name="loginsubmit" value="login" class="button"/>
		</form>
<?php
}
?>
	</div>
